package com.gitee.zhanyao.security.controller;

import com.gitee.zhanyao.api.security.api.UserApi;
import com.gitee.zhanyao.api.security.model.OAuth2User;
import com.gitee.zhanyao.api.security.model.OAuth2UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhanyao
 */
@Slf4j
@RestController
public class UserController implements UserApi {

    @Override
    public OAuth2UserDTO currentUser() {
        OAuth2User oAuth2User = (OAuth2User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        OAuth2UserDTO oAuth2UserDTO = new OAuth2UserDTO();
        BeanUtils.copyProperties(oAuth2User, oAuth2UserDTO);
        return oAuth2UserDTO;
    }

}
